from django.contrib import admin
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext as _
from mptt.admin import MPTTModelAdmin
from mptt.forms import MPTTAdminForm, TreeNodeChoiceField
from multilingual.admin import MultilingualModelAdmin
from core.tree.models import Tree
from functions.admin import AdminFunctions

class CustomMPTTModelAdmin(MultilingualModelAdmin, MPTTModelAdmin, AdminFunctions):
    use_prepopulated_fields = {'slug': ('name',)}
    list_display = ('name', 'slug', 'created', 'updated', 'link', 'active', 'actions_button')
    use_fieldsets = (
        (_('Name'), {'fields': ('name',)}),
        (_('Options'), {'fields': ('slug', 'image', 'parent',)}),
        (_('Advanced options'), {'classes': ('collapse',), 'fields': ('link', 'created', 'active',)}),
    )
    list_filter = ('link', 'active')
          
    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        from mptt.models import MPTTModel
        if issubclass(db_field.rel.to, MPTTModel):
            return super(admin.ModelAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
        return super(admin.ModelAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
        
    
admin.site.register(Tree, CustomMPTTModelAdmin)